home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.04 Apr 91 / Assoc. Arrays / TicTacToe.st < prev   
Encoding:
Text File  |  1990-04-22  |  3.7 KB  |  138 lines  |  [TEXT/MACV]

  1. GameBoard subclass: #TicTacToeGameBoard
  2.   instanceVariableNames: ''
  3.   classVariableNames: ''
  4.   poolDictionaries: '' !
  5.  
  6. !TicTacToeGameBoard class methods !
  7.  
  8. new
  9.         "create a new instance"
  10.     | aBoard |
  11.     aBoard := super new.
  12.     aBoard setWidth:3 height:3.
  13.     aBoard reset.
  14.     ^aBoard.! !
  15.  
  16.  
  17. !TicTacToeGameBoard methods !
  18.  
  19. allLegalMoves
  20.         "Answer an OrderedCollection of all legal 
  21.          moves.  For TicTacToe, any move that is not 
  22.          an occupied space is legal"
  23.     | answer |
  24.     answer := OrderedCollection new.
  25.     1 to: (width*height) do:
  26.         [:i | (positions at: i) isNil
  27.                    ifTrue: [answer add: i].
  28.         ].
  29.     ^answer.!
  30.  
  31. move: m
  32.         "Record a move by player WhoseMove.
  33.          In TicTacToe, any move into a vacant square 
  34.          is legal, and three pieces in a row wins."
  35.     | |
  36.     self loggit: ((AllPlayers at: WhoseMove) name) ,
  37.                  ' moves ' , (m printPaddedTo: 1).
  38.     (positions at: m) isNil
  39.         ifTrue: [positions at: m put: WhoseMove.
  40.                  (self threeAcross: m) |
  41.                  (self threeDown: m) |
  42.                  (self threeDiagonally: m)
  43.                     ifTrue: [^#Win]
  44.                     ifFalse: [^#Ok].
  45.                 ]
  46.         ifFalse: [^#Error].!
  47.  
  48. reset
  49.         "reset the board back to the start"
  50.     | |
  51.     positions isNil
  52.         ifTrue: 
  53.             [positions := Array new: (width * height)].
  54.     1 to: (width * height) do: 
  55.         [:i | positions at: i put: nil].!
  56.  
  57. threeAcross: aMove
  58.         "answer whether WhoseMove has three marks 
  59.          across, one of which is aMove"
  60.     | rowStart answer |
  61.     rowStart := ((aMove - 1) // 3) * 3 + 1.
  62.     answer := true.
  63.     rowStart to: (rowStart + 2) do: [ :i |
  64.         answer := answer & 
  65.                     ((positions at: i) = WhoseMove)].
  66. ^ answer.!
  67.  
  68. threeDiagonally: aMove
  69.         "answer whether WhoseMove has three marks
  70.          diagonally (aMove is not used)"
  71.     | answer1 answer2 |
  72.     answer1 := true.
  73.     answer2 := true.
  74.     1 to: 9 by: 4 do: [ :i |
  75.         answer1 := answer1 & 
  76.                     ((positions at: i) = WhoseMove)].
  77.     3 to: 7 by: 2 do: [ :i |
  78.         answer2 := answer2 & 
  79.                     ((positions at: i) = WhoseMove)].
  80. ^ (answer1 | answer2).!
  81.  
  82. threeDown: aMove
  83.         "answer whether WhoseMove has three marks down,
  84.          one of which is aMove"
  85.     | colStart answer |
  86.     colStart := (aMove - 1) \\ 3 + 1.
  87.     answer := true.
  88.     colStart to: (colStart + 6) by: 3 do: [ :i |
  89.         answer := answer & 
  90.                     ((positions at: i) = WhoseMove)].
  91. ^ answer.! !
  92.  
  93.  
  94. ComputerPlayer subclass: #TicTacToeComputerPlayer
  95.   instanceVariableNames: ''
  96.   classVariableNames: ''
  97.   poolDictionaries: '' !
  98.  
  99. !TicTacToeComputerPlayer class methods ! !
  100.  
  101.  
  102. !TicTacToeComputerPlayer methods ! !
  103.  
  104.  
  105. HumanPlayer subclass: #TicTacToeHumanPlayer
  106.   instanceVariableNames: ''
  107.   classVariableNames: ''
  108.   poolDictionaries: '' !
  109.  
  110. !TicTacToeHumanPlayer class methods ! !
  111.  
  112.  
  113. !TicTacToeHumanPlayer methods !
  114.  
  115. haveProposedMove: aMove
  116.         "Check for valid format.  The format is:
  117.          a single digit giving the space to move
  118.          to."
  119.     | moveResult |
  120.     (aMove = 'win') ifTrue: [self win. ^nil].
  121.     (aMove = 'resign' ) ifTrue: [self resign. ^nil].
  122.     (aMove size) < 1
  123.         ifTrue: [self retryMove]
  124.         ifFalse: [
  125.     (aMove at: 1) isDigit
  126.         ifTrue: [
  127.             moveResult := 
  128.                 (TheBoard move: aMove asInteger).
  129.             (moveResult = #Win)
  130.                 ifTrue: [self win]
  131.                 ifFalse:[ (moveResult = #Ok)
  132.                             ifTrue: [ self moveOver ]
  133.                             ifFalse:
  134.                                 [self retryMove].
  135.                         ]
  136.             ]
  137.         ifFalse: [self retryMove].
  138.         ]! !